package com.micro.modual.service.impl;

import com.micro.common.MicroConstant;
import com.micro.common.MicroResponseCode;
import com.micro.core.exception.BizException;
import com.micro.core.frame.service.impl.AbstractBasicsServiceImpl;
import com.micro.core.resp.RespJson;
import com.micro.core.resp.RespJsonData;
import com.micro.modual.entity.SystemDept;
import com.micro.modual.extension.SystemDeptExtension;
import com.micro.modual.mapper.SystemDeptMapper;
import com.micro.modual.mapper.SystemRoleDeptMapper;
import com.micro.modual.model.bo.systemdept.SystemDeptBO;
import com.micro.modual.model.tree.SystemDeptTreeDTO;
import com.micro.modual.service.SystemDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
*  <h1>系统部门表  实现</h1>
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:11:10
 */
@Service("systemDeptService")
public class SystemDeptServiceImpl extends AbstractBasicsServiceImpl<SystemDept,Long> implements SystemDeptService {

    @Autowired
    private SystemDeptMapper systemDeptMapper;

    @Autowired
    private SystemRoleDeptMapper systemRoleDeptMapper;

    @Override
    public RespJsonData<List<SystemDeptTreeDTO>> findSystemDeptList(Map<String, Object> params) {
        List<SystemDept> systemDeptList = systemDeptMapper.findSystemDeptList(params);
        List<SystemDeptTreeDTO> systemDeptTreeDTOList = SystemDeptExtension.converSystemDeptTree(systemDeptList);
        return new RespJsonData<List<SystemDeptTreeDTO>>(systemDeptTreeDTOList, MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    /**
     * <h2>新增系统部门表详情</h2>
     * @param systemDeptBO {@link SystemDeptBO} 系统部门表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson addSystemDept(SystemDeptBO systemDeptBO) {
        Integer deptLevel = 1;
        if(!systemDeptBO.getParentCode().equals(MicroConstant.SystemDefaultValue.DEFAULT_PARENT_CODE)){
            deptLevel = systemDeptMapper.findDeptLevelByCode(systemDeptBO.getParentCode()) + 1;
        }
        SystemDept systemDept = SystemDeptExtension.converAddSystemDept(systemDeptBO,deptLevel);
        systemDeptMapper.insertSelective(systemDept);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>修改系统部门表详情</h2>
     * @param systemDeptBO  {@link SystemDeptBO} 系统部门表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson editSystemDept(SystemDeptBO systemDeptBO) {
        Integer deptLevel = 1;
        if(!systemDeptBO.getParentCode().equals(MicroConstant.SystemDefaultValue.DEFAULT_PARENT_CODE)){
            deptLevel = systemDeptMapper.findDeptLevelByCode(systemDeptBO.getParentCode()) + 1;
        }
        SystemDept systemDept = SystemDeptExtension.converEditSystemDept(systemDeptBO,deptLevel);
        systemDeptMapper.updateByPrimaryKeySelective(systemDept);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    /**
     * <h2>根据主键编号删除系统部门表信息</h2>
     * @param id  系统部门表 主键编号
     * @return  {@link RespJson} 返回结果基础类
     */

    @Override
    public RespJson deleteSystemDept(Long id) {
        SystemDept systemDept = systemDeptMapper.selectByPrimaryKey(id);
        if(systemDeptMapper.hasChild(systemDept.getDeptCode()) > 0 ){
            throw new BizException(MicroResponseCode.DEPT_HAS_CHILD_NOT_ALLOW_DELETE.getResponseCode(),MicroResponseCode.DEPT_HAS_CHILD_NOT_ALLOW_DELETE.getResponseMessage());
        }
        if(systemRoleDeptMapper.hasDistribution(systemDept.getDeptCode()) > 0){
            throw new BizException(MicroResponseCode.DEPT_HAS_DIND_ROLE_NOT_ALLOW_DELETE.getResponseCode(),MicroResponseCode.DEPT_HAS_DIND_ROLE_NOT_ALLOW_DELETE.getResponseMessage());
        }
        systemDeptMapper.deleteByPrimaryKey(id);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    @Override
    public RespJsonData<List<SystemDeptTreeDTO>> findSystemDeptTree() {
        List<SystemDept> systemDeptList = systemDeptMapper.selectAll();
        List<SystemDeptTreeDTO> systemDeptTreeDTOList = SystemDeptExtension.converSystemDeptTree(systemDeptList);
        return new RespJsonData<List<SystemDeptTreeDTO>>(systemDeptTreeDTOList, MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

}
